<html>
    <head>
        <title>Vidracaria</title>
        <?php
        include_once 'titulo.php';
        include_once 'cabecalho.php';
        include_once 'script/conecta.php';
        include_once 'MySQL.php';
        ?>

    </head>
    <body>
        <div class="total">
            <div class="cabecalho">
            </div>

            <?php
            include_once 'menuHorizontal.php';
            ?>
            <div class="conteudo">
                <div class="subconteudo">

                    <?php
                    if (empty($_POST) || $_POST['ACAO'] == 'inserirUsuarios') {
                        inserirUsuarios();
                    }
                    ?>
                    <?php
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'listaUsuarios') {
                        listaUsuarios($_POST['PESQUISA']);
                    }
                    ?>
                    <?php
                    //SÃ³ aparece a caixa de ediÃ§Ã£o se houver um post com a aÃ§Ã£o=editarUsuario
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'updateUsuarios') {
                        updateUsuarios();
                    }
                    ?>
                    <div style="width:90%; margin-left:auto; margin-right: auto;">
                        <form action="cadastro_usuario.php" method="post">
                            <input type="hidden" name="ACAO" value="inserirUsuarios" />
                            <input type="submit" value="Novo Usuarios" class="btnAdicionar"/>
                        </form>
                    </div>
                    <?php

                    function inserirUsuarios() {
                        ?>
                        <form action="cadastro_usuario.php" method="POST">
                            <table class="tblPesquisar"><tr><td>
                                        <label class="label3">Procurar Usuarios:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                                        <input type="hidden" name="ACAO" value="listaUsuarios" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form><br/><br/>

                        <table class="tblForm">
                            <form action="script/acoes.php" method="post">
                                <input type="hidden" name="ACAO" value="inserirUsuarios"/>

                                <thead>
                                <td colspan="2">
                                <center>Cadastro de Usuarios</center></td>
                                </thead>
                                <tbody>
                                    <tr><td colspan="2">
                                <center>Preencha os campos abaixo para cadastrar Usuarios.</br>
                                    Os campos com * sÃ£o obrigatÃ³rios.</center><br/></td></tr>

                                <tr><td>
                                        <label class="label">* Nome do Usuarios</label></td><td>
                                        <input type = "text" name= "NOME" size="30px" maxlength="30"/></td></tr>

                                <tr><td>
                                        <label class="label">* Login:</label></td><td>
                                        <input type = "text" name= "LOGIN" size="30px" maxlength="20"/></td></tr>

                                <tr><td>
                                        <label class="label">* Senha:</label></td><td>
                                        <input type = "password" name= "SENHA" size="30px" maxlength="9"/></td></tr>

                                <tr><td>
                                        <label class="label">* Tipo:</label></td><td>

                                        <?php
                                        echo "<select name='TIPO'>";
                                        $sql = "SELECT *FROM tbl_tipousuario;";
                                        $resultado = executaSQL($sql);

                                        while ($linha = mysqli_fetch_array($resultado)) {
                                            echo "<option value='" . $linha['tusu_id'] . "'>" .
                                            $linha['tusu_descricao'] . "</option>";
                                        }
                                        echo "</select>";
                                        ?>
                                    </td></tr>
                                </tbody>
                                <tfoot>
                                    <tr> <td colspan="2">
                                            <input type="submit" value="CADASTRAR" class="blackButtonYes"/>
                                            <input type="reset" value="LIMPAR" class="blackButtonNo"/>
                                        </td></tr>
                                </tfoot>
                            </form>
                        </table>
                        <?php
                    }
                    ?>


                    <?php

                    function updateUsuarios() {
                        ?>
                        <form action="cadastro_usuario.php" method="POST">
                            <table class="tblPesquisar"><tr><td>
                                        <label class="label3">Procurar Usuarios:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                                        <input type="hidden" name="ACAO" value="listaUsuarios" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form><br/><br/>
                        <table class="tblForm"/>
                        <form action="script/acoes.php" method="post">
                            <input type="hidden" name="ACAO" value="updateUsuario"/>
                            <thead>
                            <td colspan="2">
                            <center>Cadastro de Usuarios</center></td>
                            </thead>
                            <tbody>
                                <tr><td colspan="2">
                            <center>Preencha os campos abaixo para cadastrar Usuarios.</br>
                                Os campos com * são obrigatórios.</center><br/></td></tr>
                            <tr><td>
                                    <label class="label">* Nome do Usuarios</label></td><td>
                                    <input type="hidden" name="ID"
                                    <?php
                                    if ($_POST['ID']) {
                                        echo " value=\"";
                                        echo $_POST['ID'];
                                        echo "\" ";
                                    }
                                    ?>
                                           />
                                    <input type = "text" name= "NOME" size="30px"
                                    <?php
                                    if ($_POST['NOME']) {
                                        echo " value=\"";
                                        echo $_POST['NOME'];
                                        echo "\" ";
                                    }
                                    ?>/></td></tr>

                            <tr><td>
                                    <label class="label">* LOGIN:</label></td><td>
                                    <input type = "text" name= "LOGIN" size="30px"
                                    <?php
                                    if ($_POST['LOGIN']) {
                                        echo " value=\"";
                                        echo $_POST['LOGIN'];
                                        echo "\" ";
                                    }
                                    ?>/></td></tr>

                            <tr><td>TIPO</td>
                                <td>
                                    <select name="TIPO">
                                        <option value="">Tipo </option>
                                        <?php
                                        $sql = "SELECT *FROM tbl_tipousuario";
                                        $resposta = executaSQL($sql);
                                        while ($linha = mysqli_fetch_array($resposta)) {
                                            ?>
                                            <option value="<?php echo $linha["tusu_id"]; ?>" <?php
                                    if ($linha["tusu_id"] == $_POST["TIPO"]) {
                                        echo "SELECTED";
                                    } else {
                                        echo "";
                                    };
                                            ?>><?php echo $linha["tusu_descricao"]; ?></option>

                                            <?php
                                        }
                                        ?>
                                    </select></td>
                            </tr>
                            </tbody>
                            <tfoot>
                            <td colspan="2">
                                <input type="submit" value="CADASTRAR" class="btnAdicionar"/>
                                <input type="reset" value="LIMPAR" class="btnLimpar"/>
                                <form action="cadastro_cliente.php" method="post"> <!--Se eu clicar aqui Ã© para cancelar o novo usuÃ¡rios e mostrar a lista-->
                                    <input type="submit" value="CANCELAR" class="btnVoltar"/>
                                </form>
                            <tfoot/>
                        </form>
                        <table/>
                        <?php
                    }
                    ?>

                    <?php

                    function listaUsuarios($pesquisa) {
                        ?>
                        <form action="cadastro_usuario.php" method="POST">
                            <table class="tblPesquisar"><tr><td>
                                        <label class="label3">Procurar Usuarios:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                                        <input type="hidden" name="ACAO" value="listaUsuarios" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form><br/><br/>
                        <?php
                        $sql = "CALL sp_getUsuariosByNome('" . $pesquisa . "')";
                        $resultado = executaSQL($sql);
                        echo "<div style='width:50%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>";
                        echo "<table class='tblUsuarios'>";
                        echo "<caption> Lista de Usuarios</caption>";
                        echo "<th>ID</th>";
                        echo "<th>NOME</th>";
                        echo "<th>LOGIN</th>";
                        echo "<th>TIPO</th>";
                        echo "<th>AÇÕES</th>";

                        while ($linha = mysqli_fetch_array($resultado)) {
                            echo "<tr>";
                            echo "<td>" . $linha['usu_id'] . "</td>";
                            echo "<td>" . $linha['usu_nome'] . "</td>";
                            echo "<td>" . $linha['usu_login'] . "</td>";
                            echo "<td>" . $linha['tusu_descricao'] . "</td>";

                            //BOTÃO EDITAR
                            echo "<td>";
                            echo "<form action='cadastro_usuario.php' method='post'>";
                            echo "<input type='hidden' name='ACAO' value='updateUsuarios'>";
                            echo "<input type='hidden' name='ID' value='" . $linha['usu_id'] . "'>";
                            echo "<input type='hidden' name='NOME' value='" . $linha['usu_nome'] . "'>";
                            echo "<input type='hidden' name='LOGIN' value='" . $linha['usu_login'] . "'>";
                            echo "<input type='hidden' name='TIPO' value='" . $linha['usu_tipo'] . "'>";
                            echo "<input type='submit' value='' class='btnEditar'>";
                            echo "</form>";


                            echo "</tr>";
                        }
                        echo "</table>";
                    }
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>

<?php

function executaSQL($sql) {
    $mySQL = new MySQL;
    $resultado = $mySQL->execSP($sql);
    return $resultado;
}
?>